package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.entity.Team;
import com.entity.User;
import com.mapper.TeamMapper;
import com.mapper.UserMapper;
import com.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;


@Service
public class TeamServiceImpl extends ServiceImpl<TeamMapper, Team> implements TeamService {

    @Autowired
    TeamMapper teamMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public Team getById(Serializable id) {
        Team t = teamMapper.selectById(id);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("team_id", id);
        List userList = userMapper.selectList(wrapper);
        t.setUserList(userList);
        return t;
    }
    //向班级添加成员
    public Team addMember(Long teamId,Long userId) {
        User user = userMapper.selectById(userId);
        user.setTeamId(teamId);
        userMapper.updateById(user);
        return this.getById(teamId);
    }
    //向班级批量添加成员
    public Team addMembers(Long teamId,List<User> userList) {
        for (int i=0;i<userList.size();i++){
            User user = userList.get(i);
            user.setTeamId(teamId);
            userMapper.updateById(user);
        }
        return this.getById(teamId);
    }
    //删除班级成员
    public int delMember(Long userId) {
        User user = userMapper.selectById(userId);
        user.setTeamId(0L);
        return userMapper.updateById(user);
    }
    //批量删除班级成员
    public Team delMembers(Long teamId,List<User> userList) {
        for (int i=0;i<userList.size();i++){
            User user = userList.get(i);
            user.setTeamId(0L);
            userMapper.updateById(user);
        }
        return this.getById(teamId);
    }
}
